<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="8">
            <a-form-model-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billNo">
              <a-input disabled v-model="model.billNo" placeholder="请输入单据编号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="注册类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="registrationCategory">
              <a-input :disabled="formDisabled || reType=='zczy'" v-model="model.registrationCategory" placeholder="请输入注册类别" :max-length="20"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" v-if="reType=='zczy'">
            <a-form-model-item label="注册专业" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="registeredProfessional">
              <a-input :disabled="formDisabled" v-model="model.registeredProfessional" placeholder="请输入注册专业" :max-length="20"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remarks">
              <a-textarea :disabled="formDisabled" :max-length="200" v-model="model.remarks" rows="4" placeholder="请输入备注"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  export default {
    name: 'ErpProfessionalManageForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{},
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
          registrationCategory: [
            { required: true, message: '请输入类别名称!', trigger: 'blur'},
          ],
        },
        url: {
          add: "/api/erp/files/professional/erpProfessionalCategory/add",
          edit: "/api/erp/files/professional/erpProfessionalCategory/edit",
          queryById: "/api/erp/files/professional/erpProfessionalCategory/queryById",
          getGainBillNo: "/api/erp/files/professional/erpProfessionalCategory/gainBillNo"
        },
        reType: 'zclb',
        failregistrationCategory: ''
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add (record,type) {
        this.reType = type
        if(type=='zczy'){
          this.$set(this.validatorRules,'registeredProfessional',[
            { required: true, message: '请输入注册专业!', trigger: 'blur'},
          ])
          this.model = Object.assign({}, this.modelDefault);
          this.$set(this.model,'parentId',record.id)
          this.$set(this.model,'registrationCategory',record.registrationCategory)
          this.failregistrationCategory = record.registrationCategory
        } else {
          this.$delete(this.validatorRules,'registeredProfessional')
          this.model = Object.assign({}, record);
        }
        this.$refs["form"].clearValidate();
        this.getBillNo(type)
        this.visible = true;
      },
      edit (record,type) {
        this.reType = type
        if(type=='zczy'){
          this.$set(this.validatorRules,'registeredProfessional',[
            { required: true, message: '请输入注册专业!', trigger: 'blur'},
          ])
        } else {
          this.$delete(this.validatorRules,'registeredProfessional')
        }
        this.$refs["form"].clearValidate();
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      getBillNo(type){
        getAction(this.url.getGainBillNo,{ type }).then(res => {
          if(res.success){
            this.$set(this.model,'billNo',res.result)
          }
        })
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            if(this.reType=='zczy'){
              this.$set(this.model,'registrationCategory',null)
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
                that.$emit('unButtonDisable');
                if(that.reType=='zczy'){
                  that.$set(this.model,'registrationCategory',that.failregistrationCategory)
                }
              }
            }).catch((error)=>{
              that.$emit('unButtonDisable');
            }).finally(() => {
              that.confirmLoading = false;
            })
          } else {
            that.$emit('unButtonDisable');
          }

        })
      },
    }
  }
</script>
